package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.domain.Admin;
import com.example.mybatisplus.mapper.AdminMapper;
import com.example.mybatisplus.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2021-04-19
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper ;
    @Override
    public Admin myGetById(Long id) {
        return adminMapper.myGetById(id);
    }

    @Override
    public Admin userLogin(Admin admin) {
        QueryWrapper<Admin> wrapper =new QueryWrapper<>();
        wrapper.eq("loginName",admin.getLoginName()).eq("password",admin.getPassword());
        Admin one =adminMapper.selectOne(wrapper);
        return  one;
    }

    @Override
    public Admin userRegister(Admin admin) {
        QueryWrapper<Admin> wrapper =new QueryWrapper<>();
        wrapper.eq("phone",admin.getPhone()).or().eq("loginName",admin.getLoginName());
        Admin one =adminMapper.selectOne(wrapper);
        return  one;
    }
    @Override
    public Admin userChange(Admin admin) {
        Admin currentUserInfo = SessionUtils.getCurrentUserInfo();
        QueryWrapper<Admin> wrapper =new QueryWrapper<>();
        wrapper.eq("phone",admin.getPhone());
        Admin one1 =adminMapper.selectOne(wrapper);
        if(one1 ==null) {
            QueryWrapper<Admin> wrapper1 =new QueryWrapper<>();
            wrapper1.eq("id", currentUserInfo.getId());
            Admin one = adminMapper.selectOne(wrapper1);
            return one;
        }else
            return null;
    }

    @Override
    public Admin pwdChange(String oldPwd) {
        Admin one;
        Admin currentUserInfo = SessionUtils.getCurrentUserInfo();
        if(currentUserInfo.getPassword().equals(oldPwd))
        {
            QueryWrapper<Admin> wrapper = new QueryWrapper<>();
            wrapper.eq("id", currentUserInfo.getId());
            one = adminMapper.selectOne(wrapper);
        }else {
            one = null;
        }
        return  one;
    }
    @Override
    public Admin userFind(Admin admin) {
        QueryWrapper<Admin> wrapper =new QueryWrapper<>();
        wrapper.eq("phone",admin.getPhone()).eq("loginName",admin.getLoginName());
        Admin one =adminMapper.selectOne(wrapper);
        return  one;
    }



}
